import java.util.Stack;

public class IsValid {
    // leetcode 有效的括号
    // https://leetcode.cn/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-100-liked
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{') stack.add(c);
            else {
                if (stack.isEmpty()) return false;
                char tmp = stack.pop();
                if (c == ')' && tmp != '('
                        || c == ']' && tmp != '['
                        || c == '}' && tmp != '{') return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        IsValid isValid = new IsValid();
        System.out.println(isValid.isValid("(]"));
    }
}
